<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class tasks_model extends CI_Model {

	function get_all(){
		$this->db->select('task.title, task.content, task.url, task.user_id, task.date_created,task.date_due, task.priority, task.status, user.first_name, user.last_name,task.responsible,task.accountable, task.consulted, task.informed');
		$this->db->from('tasks as task');
		$this->db->join('users as user', 'task.user_id=user.id');
		$query = $this->db->get();
		if ($query->num_rows > 0) {
			return $query->result();
		}else{
			return false;
		}
	}


	function get_single_task($url){
		$this->db->where('url', $url);
		$tasks_query = $this->db->get('tasks');
		if ($tasks_query->num_rows == 1) {
			foreach ($tasks_query->result() as $query) {

				$responsible_users_array = (count(json_decode($query->responsible))>0) ? $this->get_users(json_decode($query->responsible)) : false;
				$accountable_json_array = (count(json_decode($query->accountable))>0) ? $this->get_users(json_decode($query->accountable)) : false;
				$consulted_json_array = (count(json_decode($query->consulted))>0) ? $this->get_users(json_decode($query->consulted)) : false;
				$informed_json_array = (count(json_decode($query->informed))>0) ? $this->get_users(json_decode($query->informed)) : false;
				

				$owner = $this->get_user_by_id(json_decode($query->user_id));
				$query->responsible = $responsible_users_array;
				$query->accountable = $accountable_json_array;
				$query->consulted = $consulted_json_array;
				$query->informed = $informed_json_array;
				$query->owner = $owner;
				
				return $query;

			}
		}
	}

	function get_single_task_by_id($id){
		$this->db->where('id', $id);
		$tasks_query = $this->db->get('tasks');
		if ($tasks_query->num_rows == 1) {
			foreach ($tasks_query->result() as $query) {

				$responsible_users_array = (count(json_decode($query->responsible))>0) ? $this->get_users(json_decode($query->responsible)) : false;
				$accountable_json_array = (count(json_decode($query->accountable))>0) ? $this->get_users(json_decode($query->accountable)) : false;
				$consulted_json_array = (count(json_decode($query->consulted))>0) ? $this->get_users(json_decode($query->consulted)) : false;
				$informed_json_array = (count(json_decode($query->informed))>0) ? $this->get_users(json_decode($query->informed)) : false;
				

				$owner = $this->get_user_by_id(json_decode($query->user_id));
				$query->responsible = $responsible_users_array;
				$query->accountable = $accountable_json_array;
				$query->consulted = $consulted_json_array;
				$query->informed = $informed_json_array;
				$query->owner = $owner;
				
				return $query;

			}
		}
	}


	function get_user_by_id($id){
		$query = $this->db->select('first_name, last_name')->where('id', $id)->get('users');
		foreach($query->result() as $user){
			return $user->first_name . ' ' . $user->last_name;
		};
	}

	function get_users($ids){
		$data = array();
		if (is_array($ids) && count($ids) > 0) {
			foreach ($ids as $id) {
				$query = $this->db->select('id, first_name, last_name')->where('id', $id)->get('users');
				array_push($data, json_encode($query->result()));
			}
			return $data;
		}else{
			return "false";
		}
	}


	function delete($url){
		$this->db->where('url', $url);
		$this->db->delete('tasks');
		return true;
	}


	function get_tasks_from_user($user_id){
		$this->db->select('task.title, task.content, task.url, task.user_id, task.date_due, task.priority, task.status, user.first_name, user.last_name, task.responsible,task.accountable, task.consulted, task.informed');
		$this->db->from('tasks as task');
		$this->db->join('users as user', 'user.id = task.user_id');
		$this->db->where('user.id', $user_id );
 
		$query = $this->db->get();
		return $query->result();
	}	

	function insert($data){
		$query = $this->db->insert('tasks', $data);
		return true;
	}	



	function get_single_to_edit($url){
		$this->db->where('url', $url);
		$tasks_query = $this->db->get('tasks');
		if ($tasks_query->num_rows == 1) {
			foreach ($tasks_query->result() as $query) {
				
				$query->responsible = (count(json_decode($query->responsible))>0) ? $this->get_users_with_name(json_decode($query->responsible)) : false;
				$query->accountable = (count(json_decode($query->accountable))>0) ? $this->get_users_with_name(json_decode($query->accountable)) : false;
				$query->consulted = (count(json_decode($query->consulted))>0) ? $this->get_users_with_name(json_decode($query->consulted)) : false;
				$query->informed = (count(json_decode($query->informed))>0) ? $this->get_users_with_name(json_decode($query->informed)) : false;

				$query->owner = $this->get_user_by_id(json_decode($query->user_id));
				return $query;
			}
		}
	}	

	function get_users_with_name($user_id){
		$ids = implode(",", $user_id);
		$query = $this->db->query("SELECT id, CONCAT(first_name, ' ', last_name) as name FROM users WHERE id IN (". $ids . ")");
		return $query->result();
	}


	

	function update($task){
		$this->db->where('id', $task['id']);
		$this->db->update('tasks', $task);
		return ($this->db->affected_rows() > 0) ? TRUE : FALSE; 
	}


}

/* End of file tasks_model.php */
/* Location: ./application/models/tasks_model.php */

















